<template>
  <div>
    <el-card>
      <!-- input框 -->
      <el-form :inline="true" >
        <el-form-item label="关键字"  style="margin-left: 35px;"  >
          <el-input placeholder="根据编号搜素" v-model="size.keyword" clearable  size="small" >
          </el-input>
        </el-form-item>

        <div class="right" style="float:right" >
          <el-button @click="del"  size="small">清除</el-button>
          <el-button type="primary" size="small" @click="sou" >搜索</el-button>
        </div>

        <!-- 警报信息 -->
        <el-alert type="info" :title="'数据一共多少'+totbal+'条'" class="aa" show-icon close-text=" ">
        </el-alert>

      </el-form>

      <!-- table表格 -->
      <el-table :data="questionS" style="width:100%"  class="dsadas">
        <el-table-column   prop="id" label="编号" width="200px" ></el-table-column>

        <el-table-column
          prop="questionType"
          :formatter="formatter"
          label="题型"
          width="80"
        >
        </el-table-column>

        <el-table-column label="题目编号"  width="220px">
          <template slot-scope="scope">
            <el-link
              :underline="false"
              type="primary"
              v-for="(items, index) in scope.row.questionIDs"
              :key="index"
              @click="add(scope.row , index)"
              >{{ items.number }}</el-link>
          </template>
        </el-table-column>

        <el-table-column prop="addTime" label="录入时间" width="220px" >
        </el-table-column>

        <el-table-column prop="totalSeconds" label="答题时间(s)">
        </el-table-column>

        <el-table-column prop="accuracyRate" label="正确率(%)" >
        </el-table-column>

        <el-table-column prop="userName" label="录入人" >
        </el-table-column>

        <el-table-column  label="操作" >
          <template slot-scope="scope">
            <el-button
              icon="el-icon-delete"
              circle
            plain
              type="danger"
              @click="delEmp(scope.row)"

            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="clor">
        <el-pagination
          background
          :page-sizes="[10, 15, 20, 25]"
         layout="prev,pager,next,sizes,jumper"
          :total="totbal"
          :page-size="10"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>

       <!-- 题目预览 -->
      <el-dialog title="题目预览"
       :visible.sync="showDialog"
       width="70%"
       >
        <quesTionsss :userid="userId"/>

        <!-- 关闭按钮 -->

        <el-button type="primary" class="rights" @click="showDialog=false">关闭</el-button>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { questionType } from '@/api/hmmm/constants.js'
import quesTionsss from './questionsss.vue'
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
export default {
  components: {
    quesTionsss
  },
  data () {
    return {
      isfabulut: false, // 没有没有关键字
      userId: {},
      showDialog: false,
      totbal: 0, // 一共多少数据
      number: [], // number数组
      input: '',
      id: '',
      questionIDs: [],
      questionS: [], // 基础题列表
      size: {
        page: 1, // 当前页码
        pagesize: 10, // 页尺寸
        keyword: '' // 关键字
      }
    }
  },
  created () {
    this.randomsTwo()
  },
  methods: {
    add (row, index) {
      this.showDialog = true
      this.userId.row = row
      this.userId.index = index
    },

    // 获取祖题列表
    async randoms () {
      const res = await randoms(this.size)
      this.questionS = res.data.items
      this.totbal = res.data.counts
    },

    // 获取祖题列表  没有没有关键字
    async randomsTwo () {
      const res = await randoms({
        page: this.size.page,
        pagesize: this.size.pagesize
      })
      this.questionS = res.data.items
      this.totbal = res.data.counts
    },

    // 删除
    async delEmp (data) {
      // 判断是不是最后一条数据
      if (this.questionS.length === 1) {
        this.size.page--
        if (this.size.page <= 1) {
          this.size.page = 1
        }
      }
      // 重新获取数据
      // 显示删除询问对话框
      const delRes = await this.$confirm(
        '此操作将永久删除该角色, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      // 用户点击了取消，给用户进行提示
      if (delRes === 'cancel') return this.$message.info('您取消了删除')

      await removeRandoms(data)

      if (this.isfabulut) {
        this.randoms()
      } else {
        this.randomsTwo()
      }
    },

    // 每页显示的条数发生改变时触发
    handleSizeChange (newSize) {
      this.size.pagesize = newSize
      if (this.isfabulut) {
        this.randoms()
      } else {
        this.randomsTwo()
      }
    },

    // 当前页面发生改变时触发
    handleCurrentChange (newPage) {
      this.size.page = newPage
      console.log(this.isfabulut)
      if (this.isfabulut) {
        this.randoms()
      } else {
        this.randomsTwo()
      }
    },

    // 改变
    formatter (row, column, cellValue, index) {
      const obj = questionType.find((item) => item.value === Number(cellValue))
      return obj ? obj.label : ''
    },

    // 清空
    del () {
      this.size.keyword = ''
      this.randoms()
      this.isfabulut = false // 没有关键字
    },

    // 搜索
    async sou () {
      this.isfabulut = true // 有关键字
      this.size.page = 1 // 搜索页面从第一页开始
      // await randoms(this.size)
      this.randoms()
    }

  }
}
</script>

<style scoped >

.aa {
  margin-bottom: 20px;
  /* margin:10px auto ; */
  height: 35px
}
.clor {
  width: 500px;
  float: right;
  background-color: #fff;
  margin: 25px 0;
}
.rights {
  position: absolute;
  bottom: 10px;
  right: 20px;
}

</style>
